package com.gxa.service.impl;

import com.gxa.common.Q;
import com.gxa.common.R;
import com.gxa.mapper.LsFinanceMapper;
import com.gxa.pojo.*;
import com.gxa.service.LsFinanceService;
import com.gxa.utils.DateFormatUtils;
import com.gxa.utils.DateUtils;
import com.gxa.utils.LsFinanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 财务管理逻辑处理类
 * User: zdk
 * Date: 2021/12/03 16:44
 * Description:
 * Version: V1.0
 */

@Service
public class LsFinanceServiceImpl implements LsFinanceService {
    @Autowired
    private LsFinanceMapper lsFinanceMapper;

    /**
     * 财务管理信息列表查询
     * @param currentPage 页码
     * @param currentLimit 每页显示条数
     * @return 财务管理查询结果集
     */
    @Override
    public Q queryFinanceList(Integer currentPage, Integer currentLimit) {
        Integer param1=(currentPage-1)*currentLimit;
        Integer param2=currentLimit;
        List<Finance> financeList=lsFinanceMapper.queryFinanceList(param1,param2);
        Integer count = lsFinanceMapper.queryCount();
        return QueryFinanceList(financeList,count);
    }

    /**
     * 今日收支列表查询接口
     * @param currentPage  页数
     * @param currentLimit 显示行数
     * @return Q
     */
    @Override
    public Q queryTodayFinanceList(Integer currentPage, Integer currentLimit) {
        Integer param1=(currentPage-1)*currentLimit;
        Integer param2=currentLimit;
        List<Finance> financeList = lsFinanceMapper.queryTodayFinanceList(param1,param2);
        Integer count = lsFinanceMapper.queryTodayCount();
        return QueryFinanceList(financeList,count);
    }

    /**
     * 本周列表查询
     * @param currentPage  页数
     * @param currentLimit 显示行数
     * @return Q
     */
    @Override
    public Q queryWeekFinanceList(Integer currentPage, Integer currentLimit) {
        Integer param1=(currentPage-1)*currentLimit;
        Integer param2=currentLimit;
        List<Finance> financeList = lsFinanceMapper.queryWeekFinanceList(param1,param2);
        Integer count = lsFinanceMapper.queryWeekCount();
        return QueryFinanceList(financeList,count);
    }

    /**
     * 本月列表查询
     * @param currentPage  页数
     * @param currentLimit 显示行数
     * @return Q
     */
    @Override
    public Q queryMonthFinanceList(Integer currentPage, Integer currentLimit) {
        Integer param1=(currentPage-1)*currentLimit;
        Integer param2=currentLimit;
        List<Finance> financeList = lsFinanceMapper.queryMonthFinanceList(param1,param2);
        Integer count = lsFinanceMapper.queryMonthCount();
        return QueryFinanceList(financeList,count);
    }

    /**
     * 高级搜索数据查询
     * @param queryExportFinance 搜索条件
     * @return Q
     */
    @Override
    public Q queryExportFinanceList(QueryExportFinance queryExportFinance) {
        Integer param1 = (queryExportFinance.getCurrentPage()-1)*queryExportFinance.getCurrentLimit();
        Integer param2 = queryExportFinance.getCurrentLimit();
        queryExportFinance.setCurrentPage(param1);
        queryExportFinance.setCurrentLimit(param2);
        List<Finance> financeList=lsFinanceMapper.queryExportFinanceList(queryExportFinance);
        Integer count=lsFinanceMapper.queryCount();
        return QueryFinanceList(financeList,count);
    }

    /**
     * 财务管理新增收支界面下拉框查询
     * @param rae_type_no 收支类型编号
     * @return 下拉框信息结果集
     */
    @Override
    public R queryAddFinance(Integer rae_type_no) {
        QueryAddFinance queryAddFinanceList=new QueryAddFinance();
        List<Rae_Category> queryAddFinanceOneList =lsFinanceMapper.queryAddFinanceOne(rae_type_no);
        List<Employee> queryAddFinanceTwoList =lsFinanceMapper.queryAddFinanceTwo();
        List<Capital_Account> queryAddFinanceThereList = lsFinanceMapper.queryAddFinanceThere();
        List<Employee> queryAddFinanceFourList = lsFinanceMapper.queryAddFinanceFour();
        queryAddFinanceList.setRae_categoryList(queryAddFinanceOneList);
        queryAddFinanceList.setEmployeeFinanceList(queryAddFinanceTwoList);
        queryAddFinanceList.setCapital_accountList(queryAddFinanceThereList);
        queryAddFinanceList.setEmployeeSalesmanList(queryAddFinanceFourList);
        R r= new R();
        r.setCode(200);
        r.setMsg("操作成功");
        r.setData(queryAddFinanceList);
        return r;
    }

    /**
     * 新增财务管理信息接口
     * @param finance 财务管理信息实体类
     * @return 返回R 200时为操作成功
     */
    @Override
    public R addFinance(Finance finance) {
        R r=new R();
        int result=lsFinanceMapper.addFinance(finance);
        if(result==1){
            r.setCode(200);
            r.setMsg("操作成功");
            return r;
        }else {
            r.setCode(500);
            r.setMsg("系统出现错误");
            return r;
        }
    }

    /**
     * 财务合同中间表的建立接口
     * @param contract 合同管理信息
     */
    @Override
    public void addFinanceContract(FContract contract) {
        if (contract.getContract_no() != null && !"".equals(contract.getContract_no())) {
            Finance_Contract finance_Contract = new Finance_Contract();
            MiddleFinance middleFinance = new MiddleFinance();
            Finance finance = new Finance();
            finance_Contract.setFinance_Contract_Id(middleFinance.getNewContract() + middleFinance.getSymbol() + contract.getContract_no());
            finance_Contract.setContract_Id_Name(contract.getContract_no());
            lsFinanceMapper.addFinanceContract(finance_Contract);
            if (contract.getContract_receivemoney() > 0 && contract.getContract_receivemoney() != null) {
                finance.setFinance_Date(DateUtils.getCurrentDate(DateFormatUtils.DATE_FORMAT1));
                FinanceContractAggregate financeContractAggregate = new FinanceContractAggregate();
                financeContractAggregate.setContract(contract);
                financeContractAggregate.setFinance(finance);
                financeContractAggregate.setFinance_Contract(finance_Contract);
                lsFinanceMapper.addFinanceNewContract(financeContractAggregate);
            }
        }
    }

    @Override
    public void addFinancePurchase(FPurchase purchase) {
        if(purchase.getPur_id()!=null && purchase.getPur_id()>0){
            Finance_Purchase finance_Purchase = new Finance_Purchase();
            MiddleFinance middleFinance = new MiddleFinance();
            Finance finance = new Finance();
            finance_Purchase.setFinance_Purchase_Id(middleFinance.getNewPurchase()+ middleFinance.getSymbol() +String.valueOf(purchase.getPur_id()));
            finance_Purchase.setPurchase_Id_Name(purchase.getPur_id());
            lsFinanceMapper.addFinancePurchase(finance_Purchase);
            double x = 0.00d;
            BigDecimal z=BigDecimal.valueOf(x) ;
            finance.setFinance_Date(DateUtils.getCurrentDate(DateFormatUtils.DATE_FORMAT1));
            FinancePurchaseAggregste financePurchaseAggregste=new FinancePurchaseAggregste();
            financePurchaseAggregste.setFinance(finance);
            financePurchaseAggregste.setPurchase(purchase);
            financePurchaseAggregste.setFinance_Purchase(finance_Purchase);
            if(purchase.getPur_numb_Price().compareTo(z)==1 && purchase.getPur_numb_Price()!=null){
                lsFinanceMapper.addFinanceNewPurchase(financePurchaseAggregste);
            }
            if(purchase.getPur_freight().compareTo(z)==1 && purchase.getPur_freight()!=null){
                lsFinanceMapper.addFinanceNewPurchaseTraffic(financePurchaseAggregste);
            }
        }
    }

    @Override
    public void addFinanceOrders(FOrders orders) {
        if (orders.getOrder_id() != null && !"".equals(orders.getOrder_id())) {
            Finance_Orders finance_Order = new Finance_Orders();
            MiddleFinance middleFinance = new MiddleFinance();
            Finance finance = new Finance();
            finance_Order.setFinance_Orders_Id(middleFinance.getNewOrders() + middleFinance.getSymbol() + orders.getOrder_id());
            finance_Order.setOrders_Id_Name(orders.getOrder_id());
            lsFinanceMapper.addFinanceOrders(finance_Order);
            double x = 0.00d;
            BigDecimal z=BigDecimal.valueOf(x) ;
            if (orders.getOrder_received().compareTo(z)==1 && orders.getOrder_received()!= null) {
                finance.setFinance_Date(DateUtils.getCurrentDate(DateFormatUtils.DATE_FORMAT1));
                FinanceOrdersAggregate financeOrdersAggregate = new FinanceOrdersAggregate();
                financeOrdersAggregate.setOrders(orders);
                financeOrdersAggregate.setFinance(finance);
                financeOrdersAggregate.setFinance_order(finance_Order);
                lsFinanceMapper.addFinanceNewOrders(financeOrdersAggregate);
            }
        }
    }


    @Override
    public R addOrders(FOrders orders) {
        int result = lsFinanceMapper.addOrders(orders);
        return LsFinanceUtils.RUtils(result);
    }

    @Override
    public R addContract(FContract contract) {
        int result = lsFinanceMapper.addContract(contract);
        return LsFinanceUtils.RUtils(result);
    }

    @Override
    public R addPurchase(FPurchase purchase) {
        int result = lsFinanceMapper.addPurchase(purchase);
        return LsFinanceUtils.RUtils(result);
    }

    @Override
    public R UpdateStateFinance(Integer finance_no, Integer finance_state) {
        int resule = lsFinanceMapper.UpdateStateFinance(finance_no,finance_state);
        return LsFinanceUtils.RUtils(resule);
    }

    @Override
    public R UpdateFinance(Finance finance) {
        int result = lsFinanceMapper.UpdateFinance(finance);
        return LsFinanceUtils.RUtils(result);
    }

    @Override
    public Q querySearchFinance(Integer currentPage, Integer currentLimit, String search) {
        Integer param1=(currentPage-1)*currentLimit;
        Integer param2=currentLimit;
        List<Finance> financeList = lsFinanceMapper.querySearchFinance(param1,param2,search);
        Integer count =lsFinanceMapper.querySearchCount(search);
        return QueryFinanceList(financeList,count);
    }

    @Override
    public R updateDeleteFinance(Integer finance_No) {
        int result = lsFinanceMapper.updateDeleteFinance(finance_No);
        return LsFinanceUtils.RUtils(result);
    }

    @Override
    public R updateDeleteCustomerFinance(Integer finance_customer) {
        int result=lsFinanceMapper.updateDeleteCustomerFinance(finance_customer);
        return LsFinanceUtils.RUtils(result);
    }

    public  Q QueryFinanceList(List<Finance> financeList, Integer count){
        if(financeList.size() >0 && !financeList.isEmpty()){
            List<Integer> finance_no_one=new ArrayList();
            List<Integer> finance_no_two=new ArrayList();
            List<Map> financeOne = null;
            List<Map> financeTwo = null;
            if(financeList.size()>0){
                for(int i=0;i<financeList.size();i++){
                    Integer Finance_No=financeList.get(i).getFinance_No();
                    if(financeList.get(i).getFinance_Salesman()!= null && financeList.get(i).getFinance_Salesman()>0 ){
                        finance_no_one.add(Finance_No);
                    }else if(financeList.get(i).getFinance_Salesman()==0|financeList.get(i).getFinance_Salesman()==null) {
                        finance_no_two.add(Finance_No);
                    }
                }
            }
            if(finance_no_one!=null && finance_no_one.size()>0){
                financeOne = lsFinanceMapper.queryFinanceListOne(finance_no_one);
            }if (finance_no_two!= null && finance_no_two.size()>0){
                financeTwo=lsFinanceMapper.queryFinanceListTwo(finance_no_two);
            }
            FinanceList financeLists = new FinanceList();
            financeLists.setFinanceOne(financeOne);
            financeLists.setFinanceTwo(financeTwo);
            return LsFinanceUtils.QUtils(financeLists,count);
        }else {
            return LsFinanceUtils.QUtils(null,count);
        }
    }
}
